package org.example;

import it.sauronsoftware.jave.*;

import java.io.File;

/**
 * @ClassName videoTest22
 * @Description
 * @Author zhangsw
 * @Date 2020/10/20 9:31
 * @Version 1.0
 */
public class videoTest22 {
    public static void main(String[] args) {
        String path = "D:\\qmt\\imgPath\\2020-10-19\\4c3bc843-3044-4c4c-bb28-5a565778f803.mp4";
        File file = new File(path);
        String target = "D:\\qmt\\imgPath\\4c3bc843-3044-4c4c-bb28-5a565778f803.mp4";
        File targetFile = new File(target);
        ffmpegChange1(file, targetFile);
    }

    private static boolean ffmpegChange1 (File file, File fileSave){
        try{
            AudioAttributes audio = new AudioAttributes();
            audio.setCodec("libmp3lame");
            audio.setBitRate(new Integer(56000));
            audio.setChannels(new Integer(1));
            audio.setSamplingRate(new Integer(22050));

            VideoAttributes video = new VideoAttributes();
            // video.setCodec(myConfig.getFileFFmpegPath());
//            video.setCodec("libx264");
            video.setCodec("mpeg4");
            // video.setCodec("h264");
            video.setBitRate(new Integer(800000));
            video.setFrameRate(new Integer(15));

            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("mp4");
            attrs.setAudioAttributes(audio);
            attrs.setVideoAttributes(video);

            Encoder encoder = new Encoder();

            long beginTime = System.currentTimeMillis();
            MultimediaInfo m = encoder.getInfo(file);
            System.out.println(m.getDuration()/1000 + "秒");
            System.out.println("获取时长花费时间是：" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
            beginTime = System.currentTimeMillis();
//            encoder.encode(source, fileSave, attrs);

            encoder.encode(file, fileSave, attrs);
//            System.out.println("压缩完成...");
            System.out.println("压缩完成花费时间是：" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
            return true;
        }catch (EncoderException e){
            e.printStackTrace();
            System.out.println("压缩错误信息是1："+e.getMessage());
            return false;
        }
    }
}
